/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.domain.File;
import org.istt.model.FileBean;
import org.istt.model.FileReportBean;
import org.istt.web.WebConstants;

/**
 * @author Ndtho
 *
 */
public class ExportFile extends BaseReport{
	static int FONT_SIZE_8 = 8;
	static int FONT_SIZE_10 = 10;
	
	private void generateHeader(XWPFDocument docx, List<String[]> lstObj){
		
		if(lstObj.size()!=0){
			for (int i = 0; i < lstObj.size(); i++) {
				String[] obj = (String[])lstObj.get(i);
				if(obj!=null){
					XWPFParagraph para = docx.createParagraph();
					XWPFRun r = para.createRun();
					r.setBold(true);
					r.setFontFamily(FONT_TIMES_NEW_ROMAN); 
					r.setText(obj[0] + ": " +obj[1]);
				}
			}
		}
	}
	
	public InputStream toMSWord(InputStream docFile, List<File> files, List<String[]> lstObject) throws IOException {
	    
		 XWPFDocument docx = new XWPFDocument(docFile);
		    String title = "";
		    
	        /*==================== HEADER ====================*/
//		    if(lstReport!=null && lstReport.size()!=0){
//		    	for (int i = 0; i < lstReport.size(); i++) {
//		    		FileReportBean fileReportBean = (FileReportBean) lstReport.get(i);
//		    		generateTable(docx,fileReportBean);
//		    	}
//		    }
	        
		    //write to word file
		    ByteArrayOutputStream baos = new ByteArrayOutputStream();
		    docx.write(baos);
		    
		    return new ByteArrayInputStream(baos.toByteArray());
		}
		
		private void generateTable(XWPFDocument docx, FileReportBean fileReportBean){
			XWPFParagraph para = docx.createParagraph();
			
			XWPFRun r = para.createRun();
			r.setBold(true);
			r.setFontFamily(FONT_TIMES_NEW_ROMAN); 
			r.setText("PH\u00D2NG: "+fileReportBean.getDepartment().toUpperCase());
			
			//Header
	        XWPFTable tableHeader=docx.createTable();
			XWPFTableRow row = tableHeader.getRow(0);
			createTableHeader(row);
			
			List<FileBean> lstFile = fileReportBean.getLstFile();
			if(lstFile.size()!=0){
				int rowcount = 1;
				for (Iterator<FileBean> iterator = lstFile.iterator(); iterator.hasNext();) {
					FileBean fileBean = (FileBean) iterator.next();
					XWPFTableRow content_row = tableHeader.createRow();
//					generateRow(fileBean, content_row, rowcount);
					
					rowcount++;
				}
			}
		}
		
		private void generateRow(File fileBean, XWPFTableRow content_row,int rowNo){
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			
			// STT
			XWPFRun stt_column = content_row.getCell(0).getParagraphs().get(0).createRun();
			content_row.getCell(0).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(0).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			stt_column.setText(rowNo+"");
			stt_column.setFontSize(FONT_SIZE_10);
			
			// NGUOI NOP HS
			XWPFRun sender_column = content_row.getCell(1).getParagraphs().get(0).createRun();
			content_row.getCell(1).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(1).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			sender_column.setText(fileBean.getSubmitter());
			sender_column.setFontSize(FONT_SIZE_10);

			// DIA CHI CONG TRINH
			XWPFRun location_column = content_row.getCell(2).getParagraphs().get(0).createRun();
			content_row.getCell(2).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(2).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			location_column.setFontSize(FONT_SIZE_10);
			if(fileBean.getLocation()!=null)
				location_column.setText(fileBean.getLocation());
			
			// THU TUC HANH CHINH
			XWPFRun category_column = content_row.getCell(3).getParagraphs().get(0).createRun();
			content_row.getCell(3).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(3).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			category_column.setFontSize(FONT_SIZE_10);
			category_column.setText(fileBean.getCategory().getCategoryName());

			// NGAY NOP HS
			XWPFRun inputDate_column = content_row.getCell(4).getParagraphs().get(0).createRun();
			content_row.getCell(4).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(4).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			inputDate_column.setFontSize(FONT_SIZE_10);
			inputDate_column.setText(dateFormat.format(fileBean.getInputDate().toDate()));

			// NGAY NHAN PHONG CM
			XWPFRun dateCm_column = content_row.getCell(5).getParagraphs().get(0).createRun();
			content_row.getCell(5).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(5).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			dateCm_column.setFontSize(FONT_SIZE_10);
			dateCm_column.setText("");

			// NGUOI NHAN PHONG CM
			XWPFRun receiverCm_column = content_row.getCell(6).getParagraphs().get(0).createRun();
			content_row.getCell(6).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(6).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			receiverCm_column.setFontSize(FONT_SIZE_10);
			receiverCm_column.setText("");

			// the date returns the file to onegate department.
			XWPFRun dateOnegate_column = content_row.getCell(7).getParagraphs().get(0).createRun();
			content_row.getCell(7).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(7).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			dateOnegate_column.setFontSize(FONT_SIZE_10);
			dateOnegate_column.setText("");
			
			// the person who receives the file from processing department
			XWPFRun receiverOnegate_column = content_row.getCell(8).getParagraphs().get(0).createRun();
			content_row.getCell(8).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(8).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			receiverOnegate_column.setFontSize(FONT_SIZE_10);
			receiverOnegate_column.setText("");

			// the date that returns file to sender (for planing)
			XWPFRun outputDate_column = content_row.getCell(9).getParagraphs().get(0).createRun();
			content_row.getCell(9).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(9).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			outputDate_column.setFontSize(FONT_SIZE_10);
			outputDate_column.setText(dateFormat.format(fileBean.getOutputDate().toDate()));

			// NGAY LAY HS
			XWPFRun getResultDate_column = content_row.getCell(10).getParagraphs().get(0).createRun();
			content_row.getCell(10).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(10).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			getResultDate_column.setFontSize(FONT_SIZE_10);
			getResultDate_column.setText(dateFormat.format(fileBean.getDateGetResult().toDate()));

			// Person gets the file that has approved by agent 
			XWPFRun personGetResult_column = content_row.getCell(11).getParagraphs().get(0).createRun();
			content_row.getCell(11).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(11).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			personGetResult_column.setFontSize(FONT_SIZE_10);
			personGetResult_column.setText(fileBean.getReceiver());

			// The processing result of file
			XWPFRun result_column = content_row.getCell(12).getParagraphs().get(0).createRun();
			content_row.getCell(12).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(12).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			result_column.setFontSize(FONT_SIZE_10);
			result_column.setText("");
			
			// The processing result of file
			XWPFRun note_column = content_row.getCell(13).getParagraphs().get(0).createRun();
			content_row.getCell(13).getParagraphs().get(0).setSpacingAfter(SPACING_0);
			content_row.getCell(13).getParagraphs().get(0).setSpacingBefore(SPACING_06);
			note_column.setFontSize(FONT_SIZE_10);
			note_column.setText("");

		}
		
		private void createTableHeader(XWPFTableRow header_row){
			String[] headerTitle = {"T\u00EAn c\u00E1 nh\u00E2n/ c\u01A1 quan",
					"\u0110\u1ECBa \u0111i\u1EC3m",
					"Lo\u1EA1i th\u1EE7 t\u1EE5c",
					"Ng\u00E0y n\u1ED9p",
					"Ng\u00E0y nh\u1EADn (ph\u00F2ng CM)",
					"Ng\u01B0\u1EDDi nh\u1EADn (ph\u00F2ng CM)",
					"Ng\u00E0y nh\u1EADn (Ph\u00F2ng 1 c\u1EEDa)",
					"Ng\u01B0\u1EDDi nh\u1EADn (Ph\u00F2ng 1 c\u1EEDa)",
					"Ng\u00E0y h\u1EB9n tr\u1EA3",
					"Ng\u00E0y tr\u1EA3",
					"Ng\u01B0\u1EDDi nh\u1EADn",
					"K\u1EBFt qu\u1EA3 gi\u1EA3i quy\u1EBFt",
					"Ghi ch\u00FA"};

			XWPFRun stt_column = header_row.getCell(0).getParagraphs().get(0).createRun();
			stt_column.setText("STT");
			stt_column.setBold(true);
			stt_column.setFontFamily(FONT_TIMES_NEW_ROMAN); 
			stt_column.setFontSize(FONT_SIZE_10);
			
			for (int i = 0; i < headerTitle.length; i++) {
				
				header_row.createCell();
				XWPFRun header_colum = header_row.getCell(i+1).getParagraphs().get(0).createRun();
				header_row.getCell(i+1).getParagraphs().get(0).setAlignment(CENTER_ALIGN);
				header_colum.setText(headerTitle[i]);
				header_colum.setBold(true);
				header_colum.setFontFamily(FONT_TIMES_NEW_ROMAN); 
				header_colum.setFontSize(FONT_SIZE_10);

			}
		}
}
